package panel.stars
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	public class StarGroup extends Sprite
	{
		public static const WRITE_STARS:uint = 0;
		public static const READ_STARS:uint = 1;
		private var _starList:Array = [];
		private var _type:uint;
		public function StarGroup(type:uint = READ_STARS)
		{
			_type = type;
			super();
			if(type == WRITE_STARS)
			{
				Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
				this.mouseEnabled = true;
				addEvent();
				render(10);
			}else
			{
				this.mouseEnabled = false;
			}
		}
		
		private function addEvent():void
		{
			this.addEventListener(TouchEvent.TOUCH_OVER,touchOverHandler);
			this.addEventListener(TouchEvent.TOUCH_BEGIN,clickHanler);
		}
		
		protected function touchOverHandler(event:TouchEvent):void
		{
			var touchX:Number = event.stageX;
			for(var i:int=0;i<_starList.length;i++)
			{
				if(	_starList[i].x <= touchX)
				{
					_starList[i].changeType(Star.FULL_STAR);
				}else
				{
					_starList[i].changeType(Star.EMPTY_STAR);
				}
			}
		}
		
		protected function clickHanler(event:TouchEvent):void
		{
			var touchX:Number = this.mouseX;
			for(var i:int=0;i<_starList.length;i++)
			{
				if(	_starList[i].x <= touchX)
				{
					_starList[i].changeType(Star.FULL_STAR);
				}else
				{
					_starList[i].changeType(Star.EMPTY_STAR);
				}
			}
			
		}
		
		/**
		 * @param level 请输入0到10的值
		 */		
		public function set starLevel(level:uint):void
		{
			if(level > 10) return;
			render(level);
		}
		/**
		 *  
		 * @return 获取当前星级
		 * 
		 */		
		public function get starLevel():uint
		{
			var totalLevel:uint;
			for(var i:int=0;i<_starList.length;i++)
			{
				totalLevel += _starList[i].level;
			}
			return totalLevel;
		}
		
		private function render(level:uint):void
		{
			clear();
			var fullStarNum:uint = level/2;
			var halfStarNum:uint = level%2;
			var nextPos:Number = 0;
			for(var i:int=0;i<fullStarNum;i++)
			{
				var star:Star = new Star(Star.FULL_STAR);
				this.addChild(star);
				star.x = nextPos;
				nextPos = star.x + star.width;
				this.addChild(star);
				_starList.push(star);
			}
			if(halfStarNum != 0)
			{
				var halfstar:Star = new Star(Star.HALF_STAR);
				halfstar.x = nextPos;
				this.addChild(halfstar);
				_starList.push(halfstar);
			}
			if(fullStarNum != 5)
			{
				for(i=0;i<5-fullStarNum;i++)
				{
					var emptystar:Star = new Star(Star.EMPTY_STAR);
					emptystar.x = nextPos;
					nextPos = emptystar.x + emptystar.width;
					this.addChildAt(emptystar,0);
					_starList.push(emptystar);
				}
				
			}
			
		}
		
		public function clear():void
		{
			if(_type == WRITE_STARS)
			{
				this.removeEventListener(MouseEvent.CLICK,clickHanler);
				this.removeEventListener(TouchEvent.TOUCH_OVER,touchOverHandler);
			}
			for(var i:int=0;i<_starList.length;i++)
			{
				_starList[i].clear();
			}
		}
		
	}
}